import traceback
from sqlalchemy.orm import Session
from sqlalchemy.sql import func

from app.common import exceptions as excs
from app.common.enums.resp_enum import ErrTypes
from app.common.enums.bz_enum import SubAccType
from app.models.channel import ChannelFee, ChannelMerchant, PayChannel
from app.schemas.account import AccountInfo, SubAccountInfo, FullAccountInfo

import logging
logger = logging.getLogger(__name__)


def get_pay_channel(db: Session, channel_code: str):
    try:
        if not channel_code:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        pay_channel = db.query(PayChannel).filter(PayChannel.pay_channel_code == channel_code).first()
        return pay_channel
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)
    
def get_channel_fee(db: Session, channel_code: str, fee_mode: int):
    try:
        if not channel_code:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        channel_fee = db.query(ChannelFee).filter(
            ChannelFee.pay_channel_code == channel_code,
            ChannelFee.fee_mode == fee_mode
            ).first()
        return channel_fee
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)

def get_channel_fees(db: Session, channel_code: str):
    try:
        if not channel_code:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        channel_fees = db.query(ChannelFee).filter(ChannelFee.pay_channel_code == channel_code).all()
        return channel_fees
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)
    



